﻿using Dapper;
using MLSMooc.BO;
using MLSMooc.DAO.Implement.Login;
using MLSMooc.Helper;
using MLSMooc.Service.Interface.Login;
using MLSMooc.VO;
using MLSMooc.VO.Request.Login;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Threading.Tasks;

namespace MLSMooc.Service.Implement.Login
{
    public class LoginServiceImpl : ILoginService
    {
        private static LoginServiceImpl instance = new LoginServiceImpl();
        private LoginServiceImpl() { }
        public static LoginServiceImpl GetInstance() => instance;

        private static UsersDAO userDAO = UsersDAO.Getinstance();

        public async Task<(ResultModel, UsersBO)> SignIn(SignInReq data)
        {
            ResultModel result = ResultModel.GetNewInstance();

            var userNmTask = userDAO.GetByUNmAsync(data.AdminName.Trim());
            var userTask = userDAO.GetUserAsync(data.AdminName.Trim(), data.Password.Trim());
            //用户名
            UsersBO userNm = await userNmTask;
            if (userNm == null)
            {
                result.RspCode = -1;
                result.RspMsg = "该用户不存在";
                return (result, null);

            }
            else
            {
                if (userNm.isDelete == 1)
                {
                    result.RspCode = 0;
                    result.RspMsg = "该用户已被禁用";
                    return (result, userNm);
                }
            }

            UsersBO user = await userTask;
            if (user == null)
            {
                result.RspCode = 0;
                result.RspMsg = "用户名密码不匹配";
                return (result, user);
            }

            result.RspCode = 1;
            result.RspMsg = "登录成功";
            return (result, user);
        }

        public async Task<(ResultModel, UsersBO)> LocationSignIn(SignInReq data)
        {
            ResultModel result = ResultModel.GetNewInstance();

            var userNmTask = userDAO.GetByUNmAsync(data.AdminName.Trim());
            var userTask = userDAO.GetLabUserAsync(data.AdminName.Trim(), data.EncryptPwd.Trim());
            //用户名
            UsersBO userNm = await userNmTask;
            if (userNm == null)
            {
                result.RspCode = -1;
                result.RspMsg = "该用户不存在";
                return (result, null);

            }
            else
            {
                if (userNm.isDelete == 1)
                {
                    result.RspCode = 0;
                    result.RspMsg = "该用户已被禁用";
                    return (result, userNm);
                }
            }

            UsersBO user = await userTask;
            if (user == null)
            {
                result.RspCode = 0;
                result.RspMsg = "用户名密码不匹配";
                return (result, user);
            }

            result.RspCode = 1;
            result.RspMsg = "登录成功";
            return (result, user);
        }

        public async Task<(int, string)> ChengePwd(ChengePwdReq data, string userId)
        {
            UsersBO user = await userDAO.GetByIdAsync(userId);
            if (user == null)
            {
                return (0, "用户不存在");
            }

            string sql = $@"update Admin set Password=@pwd,UpdateTime=getdate() where Id=@id ";

            using (SqlConnection conn = new SqlConnection(SalesSysSqlHelper.connectionString))
            {
                await conn.ExecuteAsync(sql, new { pwd = data.New_Pwd, id = userId });
                return (1, "密码修改成功");
            }
        }




    }
}
